<template>
	<view class="addAddress absolute">
		<view class="list">
			<view class="item acea-row row-between-wrapper">
				<view class="name">发票抬头</view>
				<input type="text" placeholder="请输入发票抬头" v-model="userInvoice.invoiceName" required />
			</view>
			<view class="item acea-row row-between-wrapper">
				<view class="name">税号</view>
				<input type="text" placeholder="请输入税号" v-model="userInvoice.licenseNumber" required />
			</view>
			<view class="item acea-row row-between-wrapper">
				<view class="name">发票类型</view>
				<u-radio-group v-model="userInvoice.invoiceType" placement="row">
					<u-radio v-for="(item, index) in range":key="index" :label="item.text" :name="item.value"></u-radio>
				</u-radio-group>
			</view>
			<view class="item acea-row row-between-wrapper">
				<view class="name">邮箱</view>
				<input type="text" placeholder="请输入邮箱" v-model="userInvoice.mailbox" required />
			</view>
			<view class="item acea-row row-between-wrapper">
				<view class="name">手机号</view>
				<input type="text" placeholder="请输入手机号" v-model="userInvoice.phone" required />
			</view>
		</view>
		<view></view>
		<view class="keepBnt bg-color-red" @tap="submit">立即保存</view>
	</view>
</template>

<script type="text/babel">
	import { addInvoice ,updateInvoice} from "@/api/user";
import attrs, { required, chs_phone } from "@/utils/validate";
import { validatorDefaultCatch } from "@/utils/dialog";

export default {
  name: "AddInvoice",
  components: {},
  data() {
    return {
			value: 0,
			range: [{"value": 1,"text": "企业"	},{"value": 2,"text": "个人、非企业组织"}],
			type:'add',
			userInvoice: {
				invoiceType: 1
			}
    };
  },
  mounted: function() {
	 this.type=this.$yroute.query.details?'edit':'add'
	 this.$nextTick(()=>{
		 if(this.type=='edit'){
		  let obj=JSON.parse(this.$yroute.query.details)
		  this.userInvoice.invoiceName =  obj.invoiceName;
		  this.userInvoice.licenseNumber =  obj.licenseNumber;
		  this.userInvoice.mailbox = obj. mailbox;
		  this.userInvoice.phone =  obj.phone;
		  this.userInvoice.invoiceId=obj.invoiceId
		  this.userInvoice.invoiceType=obj.invoiceType-''
		  this.$forceUpdate()
		  uni.setNavigationBarTitle({
		      title: '修改发票'
		  })
	  }else{
		  uni.setNavigationBarTitle({
		      title: '新增发票'
		  })
	  }
	   console.log('this.userInvoice',this.$yroute.query)
	 })
    
  },
  onShow() {
	  
  },
  watch: {

  },
  methods: {
	  change(e) {
		  console.log(e)
	  },
    async submit() {
      console.log(this.userInvoice);
      let invoiceName = this.userInvoice.invoiceName,
					licenseNumber = this.userInvoice.licenseNumber,
					mailbox = this.userInvoice.mailbox,
					phone = this.userInvoice.phone;
      try {
        await this.$validator({
          invoiceName: [
            required(required.message("发票抬头"))
          ],
					licenseNumber: [
            required(required.message("税号"))
          ],
					mailbox: [
					  required(required.message("邮箱"))
					],
          phone: [
            required(required.message("手机号")),
            chs_phone(chs_phone.message())
          ]
        }).validate({ invoiceName, licenseNumber, mailbox, phone });
      } catch (e) {
        return validatorDefaultCatch(e);
      }
      try {
				console.log("----")
        let that = this;
		
		if (that.type=='edit') { 
			updateInvoice(this.userInvoice).then(function() {
				
		uni.showToast({
              title: "修改成功",
              icon: "none",
              duration: 2000
        });
			})
			
	
          } else {
			  
		addInvoice(this.userInvoice).then(function() {
			uni.showToast({
              title: "操作成功",
              icon: "none",
              duration: 2000
            });
        });
           
          }
		that.$yrouter.back();
      
      } catch (err) {
        uni.showToast({
          title: err.msg || err.response.data.msg || err.response.data.message,
          icon: "none",
          duration: 2000
        });
      }
    }
  }
};
</script>